package com.ibm.cloud.banking.trans.account;

import com.ibm.cloud.banking.facade.account.IAccountFacade;
import com.ibm.cloud.banking.facade.account.dto.AccountBalanceQueryRespDTO;
import com.ibm.cloud.banking.facade.media.IMediaCardFacade;
import com.ibm.cloud.banking.facade.media.dto.MediaCardDTO;
import com.ibm.cloud.banking.trans.account.dto.Resp111226;
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class Trans111226 extends RouteBuilder {

    @Resource
    IAccountFacade accountFacade;

    @Resource
    IMediaCardFacade mediaCardFacade;



    String PCS_CODE = "T22";
    String ROUTE_URI = "direct:T22";
    String ROUTE_DESC = "账户余额查询交易";
    String ROUTE_ID = "PCS-FLOW-T22";

    String GETTING_MEDIA_CARD_INFO = "direct:501";
    String GETTING_ACCOUNT_INFO = "direct:502";




    @Override
    public void configure() throws Exception {

        from(ROUTE_URI)
                .routeDescription(ROUTE_DESC)
                .routeId(ROUTE_ID)
                .log("开始执行账户余额查询交易。")
                .to(GETTING_MEDIA_CARD_INFO)
                .to(GETTING_ACCOUNT_INFO)
                .log("账户余额查询交易执行结束。");

        // 1. 根据卡号获取对应的账号(介质领域)
        from(GETTING_MEDIA_CARD_INFO)
                .process(exchange -> {
                    Map<String, Object> requestMsg = (LinkedHashMap<String, Object>)exchange.getIn().getBody();
                    String cardNo = (String)requestMsg.get("cardNo");
                    exchange.setProperty("card_no",cardNo);
                    exchange.getIn().setBody(cardNo, String.class);
                })
                .bean(mediaCardFacade, "getMediaCardByCardNo").log("卡信息验证");

        from(GETTING_ACCOUNT_INFO)
                .process(exchange -> {
                    MediaCardDTO mediaCardDTO = exchange.getIn().getBody(MediaCardDTO.class);
                    exchange.getIn().setBody(mediaCardDTO.getAccountNo(), String.class);
                })
                .bean(accountFacade, "getAccountBalance")
                .process(exchange -> {
                    AccountBalanceQueryRespDTO balance = exchange.getIn().getBody(AccountBalanceQueryRespDTO.class);
                    String cardNo = (String) exchange.getProperty("card_no");
                    Resp111226 response = new Resp111226();
                    response.setCardNo(cardNo);
                    response.setAccountNo(balance.getAccountNo());
                    response.setAvailableBalance(balance.getAvailableBalance());
                    response.setCustomerName(balance.getCustomerName());
                    exchange.getIn().setBody(response,Resp111226.class);
                })
                .log("账户余额查询");

        // 2. 根据账号获取账户余额
    }
}
